/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.controller.cpservice;

import algo.appMain;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import vn.webapp.controller.BaseRest;
import vn.webapp.dto.Response;
import vn.webapp.model.Classing;
import vn.webapp.model.Coursing;
import vn.webapp.model.Timetabling;
import vn.webapp.service.ClassingService;
import vn.webapp.service.CoursingService;
import vn.webapp.service.TimetablingService;
/**
 *
 * @author Tonytran
 */
@Controller("cpServiceTimetabling")
@RequestMapping(value = {"/cpservice"})
public class TimetablingController extends BaseRest {

    @Autowired
    private TimetablingService timetablingService;
    
    @Autowired
    private ClassingService classingService;
    
    @Autowired
    private CoursingService coursingService;

    @ResponseBody
    @RequestMapping(value = "deltimetabling", method = RequestMethod.POST)
    public Response delTimetabling(@RequestParam(value = "id", defaultValue = "0") int id) {
        int res = timetablingService.removeATimetabling(id);
        if (res > 0) {
            return new Response(true, "Delete successfully.", res);
        } else {
            return new Response(false, "Delete unsuccessfully. Please try again.", res);
        }
    }

    /**
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/addatimetabling", method = RequestMethod.POST)
    @ResponseBody
    public Response addATimetabling(
            @RequestParam(value = "coursingId", defaultValue = "0") int coursingId,
            @RequestParam(value = "roomId", defaultValue = "0") int roomId,
            @RequestParam(value = "day", defaultValue = "0") int day,
            @RequestParam(value = "slot", defaultValue = "0") int slot,
            @RequestParam(value = "timerange", defaultValue = "0") int timerange) {
        
        if(coursingId != 0)
        {
            Set<Coursing> coursings = coursingService.getById(coursingId);
            Coursing abc = new Coursing();
            for(Coursing coursing : coursings)
            {
                abc = coursing;
            }

            // Return data back to ajax calling
            int timetablingId = 0;
            if(coursings != null)
            {
                timetablingId = timetablingService.saveATimetabling(abc, roomId, day, slot, timerange);

            }

            if(timetablingId > 0){
                return new Response(true, "Save successfully.", timetablingId);               
            }
            else{ 
                return new Response(false, "Save unsuccessfully. Please retry", 0);
            }
        }else
        {
            return new Response(false, "Course teacher can not be null", 0);
        }
    }
    
    /**
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/timetablingdetail", method = RequestMethod.POST)
    @ResponseBody
    public Response editATimetabling(
            @RequestParam(value = "id", defaultValue = "0") int id,
            @RequestParam(value = "coursingId", defaultValue = "0") int coursingId,
            @RequestParam(value = "roomId", defaultValue = "0") int roomId,
            @RequestParam(value = "day", defaultValue = "0") int day,
            @RequestParam(value = "slot", defaultValue = "0") int slot,
            @RequestParam(value = "timerange", defaultValue = "0") int timerange) {
        Set<Coursing> coursings = coursingService.getById(coursingId);
        Coursing abc = new Coursing();
        for(Coursing coursing : coursings)
        {
            abc = coursing;
        }
        int o_Exists = timetablingService.checkExists(id, abc, coursingId, roomId, day, slot);
        if(o_Exists == 0){
            // Return data back to ajax calling
            int timetablingId = 0;
            if(coursings != null)
            {
                //timetablingId = timetablingService.saveATimetabling(abc, roomId, day, slot);
                timetablingService.editATimetabling(id, abc, coursingId, roomId, day, slot, timerange);

            }
            // Return data back to ajax calling
            return new Response(true, "Edit successfully.", 1);
        }else
        {
            return new Response(false, "This record has been existed.", 0);
        }
    }
    
    
    @RequestMapping(value = "/runalgo", method = RequestMethod.POST)
    @ResponseBody
    public Response runAlgorithm(){
        try{
            //appMain a = new appMain();
            appMain o_appMain = new appMain();
            String message = o_appMain.test();
            //String args = "algorithm";
            //appMain.main(args);
            return new Response(true, "Run algorithm successfully!", 1);
        }catch(Exception e)
        {
            e.getStackTrace();
            return new Response(false, e.getMessage(), 0);
        }
    }
}
